<template>
    <div class="home">
        <h2>类型断言</h2>
    </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
    setup() {
        // 类型断言
        // 写法1：<类型>变量名
        // 写法2：变量名 as 类型

        function aaa(e: number | string): number {
            console.log(e);
            // <string>e 类型断言为字符串，假设是字符串，解决错误的提示信息
            if ((e as string).length) {
                // return (<string>e).length;
                return (e as string).length;
            } else {
                return e.toString().length;
            }
        }
        console.log(aaa(111111));
        console.log(aaa("你大爷"));

        return {};
    },
});
</script>
